package com.IntelligenceInsight.Myth.controller;

import com.IntelligenceInsight.Myth.model.User;
import com.IntelligenceInsight.Myth.service.UserService;
import com.IntelligenceInsight.Myth.util.NoteRequest;
import com.sun.net.httpserver.HttpServer;
import javafx.beans.binding.When;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@RestController
public class UserController {
    @Autowired
    private UserService service;

   @RequestMapping(value = {"/login.do"}, method = RequestMethod.POST)
   @ResponseBody
   public ModelAndView Login( @RequestParam String username,
                             @RequestParam String password,
                              HttpServletRequest request) throws IOException {
       ModelAndView mv = new ModelAndView();
      // NoteRequest res = service.Login(username, password);
//       if(res.getStatus() == 0){
//           User user = (User) res.getData();
//           mv.setViewName("page/home");
//           mv.addObject( "username",username);
//           HttpSession session = request.getSession();
//           session.setAttribute("user", user);
//           session.setAttribute("username", username);
//       } else {
//           mv.setViewName("index");
//           mv.addObject("err", "用户信息错误，请重新输入！");
//       }
      // User user = (User) res.getData();
       mv.setViewName("page/home");
       mv.addObject( "username",username);
       HttpSession session = request.getSession();
     //  session.setAttribute("user", user);
       session.setAttribute("username", username);
       return mv;
   }

    @RequestMapping("/login")
    public ModelAndView jumpLoginHtml(HttpSession session, ModelMap model){
        return new ModelAndView("/page/login");
    }
    @RequestMapping("/page/home")
    public ModelAndView jumpHome(HttpSession session, ModelMap model){
        return new ModelAndView("/page/home");
    }
    @RequestMapping("/page/home1")
    public ModelAndView jumpHome1(HttpSession session, ModelMap model){
        return new ModelAndView("/page/home1");
    }

    @RequestMapping("/page/mue")
    public ModelAndView jumpMue(HttpSession session, ModelMap model){
        return new ModelAndView("/page/mue");
    }

    @RequestMapping("/page/job")
    public ModelAndView jumpJob(HttpSession session, ModelMap model, HttpServletRequest request){
        String modelname=request.getParameter("modelname");
        String uuid=request.getParameter("modelId");
        ModelAndView mv= new ModelAndView("page/job");
        mv.addObject("modelname", modelname);
        mv.addObject("uuid", uuid);
        //mv.addObject( "id",id);
        return mv;
    }
    @RequestMapping("/page/AHCRBF")
    public ModelAndView jumpAHCRBF(HttpSession session, ModelMap model, HttpServletRequest request){
        String modelname=request.getParameter("modelname");
        String uuid=request.getParameter("modelId");
        ModelAndView mv= new ModelAndView("page/AHCRBF");
        mv.addObject("modelname", modelname);
        mv.addObject("uuid", uuid);
        //mv.addObject( "id",id);
        return mv;
    }
    @RequestMapping("/page/GRAHMM")
    public ModelAndView jumpGRAHMM(HttpSession session, ModelMap model, HttpServletRequest request){
        String modelname=request.getParameter("modelname");
        String uuid=request.getParameter("modelId");
        ModelAndView mv= new ModelAndView("page/GRAHMM");
        mv.addObject("modelname", modelname);
        mv.addObject("uuid", uuid);
        //mv.addObject( "id",id);
        return mv;
    }
    @RequestMapping("/page/GRAISM")
    public ModelAndView jumpGRAISM(HttpSession session, ModelMap model, HttpServletRequest request){
        String modelname=request.getParameter("modelname");
        String uuid=request.getParameter("modelId");
        ModelAndView mv= new ModelAndView("page/GRAISM");
        mv.addObject("modelname", modelname);
        mv.addObject("uuid", uuid);
        //mv.addObject( "id",id);
        return mv;
    }
    @RequestMapping("/page/LR")
    public ModelAndView jumpLR(HttpSession session, ModelMap model, HttpServletRequest request){
        String modelname=request.getParameter("modelname");
        String uuid=request.getParameter("modelId");
        ModelAndView mv= new ModelAndView("page/LR");
        mv.addObject("modelname", modelname);
        mv.addObject("uuid", uuid);
        //mv.addObject( "id",id);
        return mv;
    }

    @RequestMapping("/page/SVM")
    public ModelAndView jumpSVM(HttpSession session, ModelMap model, HttpServletRequest request){
        String modelname=request.getParameter("modelname");
        String uuid=request.getParameter("modelId");
        ModelAndView mv= new ModelAndView("page/SVM");
        mv.addObject("modelname", modelname);
        mv.addObject("uuid", uuid);
        //mv.addObject( "id",id);
        return mv;
    }
    @RequestMapping("/page/KNN")
    public ModelAndView jumpKNN(HttpSession session, ModelMap model, HttpServletRequest request){
        String modelname=request.getParameter("modelname");
        String uuid=request.getParameter("modelId");
        ModelAndView mv= new ModelAndView("page/KNN");
        mv.addObject("modelname", modelname);
        mv.addObject("uuid", uuid);
        //mv.addObject( "id",id);
        return mv;
    }
    @RequestMapping("/page/linearRegression")
    public ModelAndView jumplinearRegression(HttpSession session, ModelMap model, HttpServletRequest request){
        String modelname=request.getParameter("modelname");
        String uuid=request.getParameter("modelId");
        ModelAndView mv= new ModelAndView("page/linearRegression");
        mv.addObject("modelname", modelname);
        mv.addObject("uuid", uuid);
        //mv.addObject( "id",id);
        return mv;
    }

    @RequestMapping("/page/feature")
    public ModelAndView jumpfeature(HttpSession session, ModelMap model, HttpServletRequest request){
        String modelname=request.getParameter("modelname");
        String uuid=request.getParameter("modelId");
        ModelAndView mv= new ModelAndView("page/feature");
        mv.addObject("modelname", modelname);
        mv.addObject("uuid", uuid);
        //mv.addObject( "id",id);
        return mv;
    }
    @RequestMapping("/page/task")
    public ModelAndView jumptask(HttpSession session, ModelMap model, HttpServletRequest request){
        String modelname=request.getParameter("modelname");
        String uuid=request.getParameter("modelId");
        ModelAndView mv= new ModelAndView("page/task");
        mv.addObject("modelname", modelname);
        mv.addObject("uuid", uuid);
        //mv.addObject( "id",id);
        return mv;
    }
    @RequestMapping("/page/Kmeans")
    public ModelAndView jumpKmeans(HttpSession session, ModelMap model, HttpServletRequest request){
        String modelname=request.getParameter("modelname");
        String uuid=request.getParameter("modelId");
        ModelAndView mv= new ModelAndView("page/Kmeans");
        mv.addObject("modelname", modelname);
        mv.addObject("uuid", uuid);
        //mv.addObject( "id",id);
        return mv;
    }
    @RequestMapping("/page/PCA")
    public ModelAndView jumpPCA(HttpSession session, ModelMap model, HttpServletRequest request){
        String modelname=request.getParameter("modelname");
        String uuid=request.getParameter("modelId");
        ModelAndView mv= new ModelAndView("page/PCA");
        mv.addObject("modelname", modelname);
        mv.addObject("uuid", uuid);
        //mv.addObject( "id",id);
        return mv;
    }



    @RequestMapping("/page/jobList")
    public ModelAndView jumpJobList(HttpSession session, ModelMap model){
        return new ModelAndView("page/jobList");
    }

    @RequestMapping(value = {"/page/model"})
    public ModelAndView jumpModel(HttpSession session, ModelMap model, HttpServletRequest request)throws IOException {
        String modelname=request.getParameter("modelname");
        String uuid=request.getParameter("uuid");
        String modelString=request.getParameter("modelString");
        ModelAndView mv= new ModelAndView("page/model");
        mv.addObject("modelname", modelname);
        mv.addObject("uuid", uuid);
        mv.addObject("modelString", modelString);
        return mv;
    }

    @RequestMapping("/index")
    public ModelAndView jumpLogin(HttpSession session, ModelMap model){
        return new ModelAndView("index");
    }

    @RequestMapping("/page/modelList")
    public ModelAndView jumpModelList(HttpSession session, ModelMap model){
        return new ModelAndView("page/modelList");
    }

    @RequestMapping("/page/MCAlgorithm")
    public ModelAndView jumpMCAlgorithm(HttpSession session, ModelMap model){
        return new ModelAndView("page/MCAlgorithm");
    }

    @RequestMapping("/page/MLJoblist")
    public ModelAndView jumpMLJoblist(HttpSession session, ModelMap model){
        return new ModelAndView("page/MLJoblist");
    }

    @RequestMapping("/page/creatMLAlgorithm")
    public ModelAndView jumpcreatMLAlgorithm(HttpSession session, ModelMap model){
        return new ModelAndView("page/creatMLAlgorithm");
    }

    @RequestMapping(value = {"/getUserListAll.do"}, method = RequestMethod.POST)
    public NoteRequest UserListAll(HttpServletRequest request) {
        NoteRequest res = new NoteRequest();
        res = service.UserListAll();

//      When userType==9, who can get all user info
        if(request.getSession().getAttribute("userType").equals(9)) {
            res = service.UserListAll();
        } else {
            res.setStatus(1);
            res.setMsg("Permission denied");
            res.setData(null);
        }
        return res;
    }


    @RequestMapping(value = {"/getUserListByUserType.do"}, method = RequestMethod.POST)
    public NoteRequest UserListByUserType(HttpServletRequest request) {
        NoteRequest res = new NoteRequest();
        int userType = Integer.parseInt(request.getParameter("userType"));
        res = service.UserListByUserType(userType);
        return res;
    }


    @RequestMapping(value = {"/userAdd.do"}, method = RequestMethod.POST)
    public NoteRequest UserAdd(HttpServletRequest request) {
        NoteRequest res = new NoteRequest();

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String passwordAgain = request.getParameter("passwordAgain");
        String userTypes = request.getParameter("userType");
        String realName = request.getParameter("realName");
        String email = request.getParameter("email");

        if(username.isEmpty() || password.isEmpty() || passwordAgain.isEmpty() || userTypes.isEmpty()) {
            res.setStatus(1);
            res.setMsg("Username or password or userType is empty");
            return res;
        }

        if(!password.equals(passwordAgain)) {
            res.setStatus(1);
            res.setMsg("Password is not equal passwordAgain");
            return res;
        }

        int userType = Integer.parseInt(userTypes);
        res = service.UserAdd(username, password, userType, email, realName);

        return res;
    }
}
